﻿// using System.Diagnostics;
// using System.Globalization;
//
// namespace app1;
//
// public class NumberF
// {
//     private const int CharStackBufferSize = 32;
//     
//     public static string FormatDouble(double value, string? format, NumberFormatInfo info)
//     {
//         var vlb = new ValueListBuilder<char>(stackalloc char[CharStackBufferSize]);
//         string result = FormatDouble(ref vlb, value, format, info) ?? vlb.AsSpan().ToString();
//         vlb.Dispose();
//         return result;
//     }
//     
//     private static unsafe string? FormatDouble<TChar>(ref ValueListBuilder<TChar> vlb, double value, ReadOnlySpan<char> format, NumberFormatInfo info) where TChar : unmanaged, IUtfChar<TChar>
//         {
//             if (!double.IsFinite(value))
//             {
//                 if (double.IsNaN(value))
//                 {
//                     if (typeof(TChar) == typeof(char))
//                     {
//                         return info.NaNSymbol;
//                     }
//                     else
//                     {
//                         vlb.Append(info.NaNSymbolTChar<TChar>());
//                         return null;
//                     }
//                 }
//
//                 if (typeof(TChar) == typeof(char))
//                 {
//                     return double.IsNegative(value) ? info.NegativeInfinitySymbol : info.PositiveInfinitySymbol;
//                 }
//                 else
//                 {
//                     vlb.Append(double.IsNegative(value) ? info.NegativeInfinitySymbolTChar<TChar>() : info.PositiveInfinitySymbolTChar<TChar>());
//                     return null;
//                 }
//             }
//
//             char fmt = ParseFormatSpecifier(format, out int precision);
//             byte* pDigits = stackalloc byte[DoubleNumberBufferLength];
//
//             if (fmt == '\0')
//             {
//                 // For back-compat we currently specially treat the precision for custom
//                 // format specifiers. The constant has more details as to why.
//                 precision = DoublePrecisionCustomFormat;
//             }
//
//             NumberBuffer number = new NumberBuffer(NumberBufferKind.FloatingPoint, pDigits, DoubleNumberBufferLength);
//             number.IsNegative = double.IsNegative(value);
//
//             // We need to track the original precision requested since some formats
//             // accept values like 0 and others may require additional fixups.
//             int nMaxDigits = GetFloatingPointMaxDigitsAndPrecision(fmt, ref precision, info, out bool isSignificantDigits);
//
//             if ((value != 0.0) && (!isSignificantDigits || !Grisu3.TryRunDouble(value, precision, ref number)))
//             {
//                 Dragon4Double(value, precision, isSignificantDigits, ref number);
//             }
//
//             number.CheckConsistency();
//
//             // When the number is known to be roundtrippable (either because we requested it be, or
//             // because we know we have enough digits to satisfy roundtrippability), we should validate
//             // that the number actually roundtrips back to the original result.
//
//             Debug.Assert(((precision != -1) && (precision < DoublePrecision)) || (BitConverter.DoubleToInt64Bits(value) == BitConverter.DoubleToInt64Bits(NumberToFloat<double>(ref number))));
//
//             if (fmt != 0)
//             {
//                 if (precision == -1)
//                 {
//                     Debug.Assert((fmt == 'G') || (fmt == 'g') || (fmt == 'R') || (fmt == 'r'));
//
//                     // For the roundtrip and general format specifiers, when returning the shortest roundtrippable
//                     // string, we need to update the maximum number of digits to be the greater of number.DigitsCount
//                     // or DoublePrecision. This ensures that we continue returning "pretty" strings for values with
//                     // less digits. One example this fixes is "-60", which would otherwise be formatted as "-6E+01"
//                     // since DigitsCount would be 1 and the formatter would almost immediately switch to scientific notation.
//
//                     nMaxDigits = Math.Max(number.DigitsCount, DoublePrecision);
//                 }
//                 NumberToString(ref vlb, ref number, fmt, nMaxDigits, info);
//             }
//             else
//             {
//                 Debug.Assert(precision == DoublePrecisionCustomFormat);
//                 NumberToStringFormat(ref vlb, ref number, format, info);
//             }
//             return null;
//         }
// }